// SendMessage.java - Sample application.
//
// This application shows you the basic procedure for sending messages.
// You will find how to send synchronous and asynchronous messages.
//
// For asynchronous dispatch, the example application sets a callback
// notification, to see what's happened with messages.

package com.qp.smsmarketing.utils.modem;

import com.qp.smsmarketing.view.mbean.OpcoesMBean;
import java.io.IOException;
import java.util.Enumeration;
import javax.swing.JOptionPane;
import org.smslib.AGateway;
import org.smslib.GatewayException;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.helper.CommPortIdentifier;
import org.smslib.modem.SerialModemGateway;

public class SendMessage
{
    private SerialModemGateway serialModemGatewayConn;

        public void doIt() throws Exception
	{
		OutboundNotification outboundNotification = new OutboundNotification();
		System.out.println("Example: Send message from a serial gsm modem.");
		System.out.println(Library.getLibraryDescription());
		System.out.println("Version: " + Library.getLibraryVersion());
		SerialModemGateway gateway = new
                        SerialModemGateway("claro.com.br", "COM5", 115200, "D-Link", "DWM-156");

                gateway.setInbound(true);
		gateway.setOutbound(true);
		gateway.setSimPin("0000");
		// Explicit SMSC address set is required for some modems.
		// Below is for VODAFONE GREECE - be sure to set your own!
		gateway.setSmscNumber("+306942190000");
		Service.getInstance().setOutboundMessageNotification(outboundNotification);
		Service.getInstance().addGateway(gateway);
		Service.getInstance().startService();
		System.out.println();
		System.out.println("Modem Information:");
		System.out.println("  Manufacturer: " + gateway.getManufacturer());
		System.out.println("  Model: " + gateway.getModel());
		System.out.println("  Serial No: " + gateway.getSerialNo());
		System.out.println("  SIM IMSI: " + gateway.getImsi());
		System.out.println("  Signal Level: " + gateway.getSignalLevel() + " dBm");
		System.out.println("  Battery Level: " + gateway.getBatteryLevel() + "%");
		System.out.println();
		// Send a message synchronously.
		OutboundMessage msg = new OutboundMessage("559887516826", "Hello from SMSLib!");
		Service.getInstance().sendMessage(msg);
		System.out.println(msg);
		// Or, send out a WAP SI message.
		//OutboundWapSIMessage wapMsg = new OutboundWapSIMessage("306974000000",  new URL("http://www.smslib.org/"), "Visit SMSLib now!");
		//Service.getInstance().sendMessage(wapMsg);
		//System.out.println(wapMsg);
		// You can also queue some asynchronous messages to see how the callbacks
		// are called...
		//msg = new OutboundMessage("309999999999", "Wrong number!");
		//srv.queueMessage(msg, gateway.getGatewayId());
		//msg = new OutboundMessage("308888888888", "Wrong number!");
		//srv.queueMessage(msg, gateway.getGatewayId());
		System.out.println("Now Sleeping - Hit <enter> to terminate.");
		System.in.read();
		Service.getInstance().stopService();
	}

    /**
     * @return the serialModemGatewayConn
     */
    public SerialModemGateway getSerialModemGatewayConn() {
        return serialModemGatewayConn;
    }

    /**
     * @param serialModemGatewayConn the serialModemGatewayConn to set
     */
    public void setSerialModemGatewayConn(SerialModemGateway serialModemGatewayConn) {
        this.serialModemGatewayConn = serialModemGatewayConn;
    }

	public class OutboundNotification implements IOutboundMessageNotification
	{
		public void process(AGateway gateway, OutboundMessage msg)
		{
			System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId());
			System.out.println(msg);
		}
	}

	public static void main(String args[])
	{
		SendMessage app = new SendMessage();
		try
		{
			app.doIt();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

        public boolean conectarModem()
        {
           Enumeration<CommPortIdentifier> en = CommPortIdentifier.getPortIdentifiers();
           SendMessage sm = new SendMessage();
           OpcoesMBean opcoesmodem = new OpcoesMBean();
           boolean conectou = false;
           while(en.hasMoreElements())
            {
                String port = en.nextElement().getName();
                if( port.startsWith("COM"))
                {
                    System.out.println(port);
                   String res = sm.connModem(port,opcoesmodem.getStringConfigModem("SimPin"));
                   if(res.equals("ok"))
                   {
                       conectou = true;
                       break;
                   }
                   else
                   {
                       JOptionPane.showMessageDialog(null,res,"Erro de conexão com o modem",1);
                   }
                }
            }
           return conectou;
        }

        private String connModem(String portc,String simpin)
        {
            try
            {
               // OutboundNotification outboundNotification = new OutboundNotification();
                    SerialModemGateway gateway = new
                            SerialModemGateway("claro.com.br", portc, 115200, "D-Link", "DWM-156");

                 //   gateway.setInbound(true);
                   // gateway.setOutbound(true);
                    gateway.setSimPin(simpin);
                    // Explicit SMSC address set is required for some modems.
                    // Below is for VODAFONE GREECE - be sure to set your own!
                    gateway.setSmscNumber("+306942190000");
                    //Service.getInstance().setOutboundMessageNotification(outboundNotification);
                    Service.getInstance().addGateway(gateway);
                    Service.getInstance().startService();
                    setSerialModemGatewayConn(gateway);
                    return "ok";
            }
            catch(GatewayException ge)
            {
                return ge.toString();
            }
            catch(SMSLibException sms)
            {
                return sms.toString();
            }
            catch(IOException io)
            {
                return io.toString();
            }
            catch(InterruptedException ie)
            {
                return ie.toString();
            }
        }
}
